<template>
	<el-col :span="24" class="box">
		<el-col :span="24" class="box-header">审核人列表</el-col>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="warning" @click="fnOpenAuditor">新增审核人</el-button>
				</el-form-item>
				<el-form-item>
					<el-button @click="emitGoBack">返回</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--审核人列表-->
		<el-table :data="auditors" border v-loading="auditorsLoading" class="auditor-list m-l-3">
			<el-table-column prop="EmpID" label="工号" width="232">
			</el-table-column>
			<el-table-column prop="EmpName" label="姓名" width="232">
			</el-table-column>
			<el-table-column prop="Createdby" label="创建人" width="232">
			</el-table-column>
			<el-table-column prop="CreatedTime" label="创建时间" width="251">
			</el-table-column>
			<el-table-column label="操作" width="65">
				<template scope="scope">
				    <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
				      	<el-button type="danger" size="mini" icon="custom-delete"  @click="fnDelete(scope.row.ID)"></el-button>
				    </el-tooltip>
				</template>
			</el-table-column>
		</el-table>
		
		<!--新增办事员-->
		<el-dialog title="新增审核人" v-model="addFormVisible" :close-on-click-modal="false" class="add-dialog">
			<el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
				<el-form-item label="部门">
					<el-select v-model="addForm.dept" placeholder="请选择" style="width: 42%;" @change="fnChangeDept">
						<el-option v-for="item in depts" :key="item.ORGANIZATION_ID" :label="item.ORGANIZATION_NAME" :value="item.ORGANIZATION_ID"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="人员">
					<el-select v-model="addForm.account" placeholder="请选择" style="width: 42%;">
						<el-option v-for="item in staffs" :key="item.USER_ACCOUNT" :label="item.USER_NAME" :value="item.USER_ACCOUNT">
							<span style="float: left">{{ item.USER_NAME }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.USER_ACCOUNT }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="fnAddAuditor">提交</el-button>
			</div>
		</el-dialog>
	</el-col>
</template>

<script>
	import Svc from '../../service/index.js'
	import moment from 'moment'
	export default {
		props:['office-id'],
		data() {
			return {
				auditorsLoading : false,
				auditors : [],
				addFormVisible : false,
				//新增内容
				addForm : {
					dept : '',
					account :'',
					title : ''
				},
				//新增验证
				addFormRules : {},
				depts : [], //所有部门
				staffs : [] //当前部门下的员工
			}
		},
		methods: {
			//自定义事件->返回
			emitGoBack(){
				this.$emit('go-back','room','auditOffice')
			},
            //显示message提示框
            openMessage : function(msg,type){
    	        this.$message({
		          	showClose: true,
		          	message: msg,
		          	type: type
		        });
		    },
		    //获取部门下所有审核人
		    fnGetAuditors(){
		    	var that = this
	            Svc.fnGetData('/api/mroom/office/approver/fetch/'+that.officeId,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.auditors = res.body.Data
	            	} else {
	            		that.openMessage("当前部门的审核人获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //获取所有部门信息
		    fnGetDepts(){
		    	var that = this
	            Svc.fnGetData('/api/account/alldepts',{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.depts = res.body.Data
	            	} else {
	            		that.openMessage("部门信息获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //选择部门
		    fnChangeDept(val){
		    	var that = this
		    	that.fnGetStaffs()
		    },
		    //获取当前部门的员工
		    fnGetStaffs(){
		    	var that = this
	            Svc.fnGetData('/api/account/deptuser/'+that.addForm.dept,{},function(res){
	            	if (res.body.ErrCode == 0){
	            		that.staffs = res.body.Data
	            	} else {
	            		that.openMessage("当前部门的员工信息获取失败！",'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //打开新增审核人页面
		    fnOpenAuditor(){
		    	var that = this
		    	that.addFormVisible = true
		    },
		    //新增审核人
	      	fnAddAuditor(){
	      		var that = this
		    	var params = {
		    		OfficeID : that.officeId,
		    		EmpID : that.addForm.account,
					EmpName : ''
		    	}
		    	//获取审核人姓名
		    	for (var i=0;i<that.staffs.length;i++) {
		    		var item = that.staffs[i]
		    		if (item.USER_ACCOUNT == that.addForm.account) {
		    			params.EmpName = item.USER_NAME
		    			break
		    		}
		    	}
		    	
	            Svc.fnPostData('/api/mroom/office/approver/add',params,function(res){
	            	if (res.body.ErrCode == 0){
	            		that.addFormVisible = false
	            		that.fnGetAuditors()
	            	} else {
	            		that.openMessage("新增审核人失败！" + res.body.ErrMsg,'error')
	            	}
	            },function(res){
	            	that.openMessage("网络错误，请重新！",'error')
	            })
		    },
		    //删除
		    fnDelete(id){
		    	var that = this
		    	that.$confirm('您确认删除该审核员吗？', '提示', {
					type: 'warning'
				}).then(function(){
					Svc.fnPostData('/api/mroom/office/approver/remove',{ID:id},function(res){
		            	if (res.body.ErrCode == 0){
		            		that.openMessage("删除操作成功！",'success')
		            		that.fnGetAuditors()
		            	} else {
		            		that.openMessage("删除操作失败！",'error')
		            	}
		            },function(res){
		            	that.openMessage("网络错误，请重新！",'error')
		            })
				})
		    }
		},
		mounted() {
			//获取部门下所有审核人
		    this.fnGetAuditors()
		    //获取所有部门信息
		    this.fnGetDepts()
		}
	}
</script>

<style scoped>
	.m-l-3{
		margin-left: 3%;
	}
	.box {
		background-color: #fff;
   		box-sizing: border-box;
   		background: #fff;
    	border: 1px solid #d1dbe5;
	    box-shadow: 0 2px 4px 0 rgba(0,0,0,.12), 0 0 6px 0 rgba(0,0,0,.04);
		margin-top: 20px;
	}

	.box-header{
		background-color: #eef1f6;
	    border-bottom: 1px solid #d1dbe5;
	    padding: 10px;
	    margin-bottom: 24px;
	    font-size: 15px;
	    color: #48576a;
	}
	.auditor-list{
		width: 94%;
		margin-bottom:30px;
	}
	
	.toolbar {
		width: 94%;
		height: 56px;
	    background: #f2f2f2;
	    padding: 10px 10px 0 10px;
	    margin: 10px 0 20px 3%;
	}
</style>